<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>keepalived 和 lvs 的结合配置文件详解 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>keepalived 和 lvs 的结合配置文件详解</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/LVS/">LVS</a> <a class="article__tag-link" href="/tags/keepalived/">keepalived</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;/export/servers/keepalived/conf/keepalived.conf配置文件详解：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div></pre></td><td class="code"><pre><div class="line">! Configuration File <span class="keyword">for</span> keepalived</div><div class="line"><span class="comment">#全局配置</span></div><div class="line">global_defs &#123;</div><div class="line">notification_email &#123; <span class="comment">#设置报警邮件地址，每行一个，（如何要开启邮件报警，需要开启本机的sendmail服务）</span></div><div class="line">lihuiyw@jd.com</div><div class="line">&#125;</div><div class="line">notification_email_from lihuiyw@jd.com <span class="comment">#设置邮件的发送地址</span></div><div class="line">smtp_server mail.jd.com <span class="comment">#设置邮件的smtp server地址</span></div><div class="line">smtp_connect_timeout 30 <span class="comment">#设置连接smtp server的超时时间</span></div><div class="line">router_id LVS_DEVEL <span class="comment">#表示keepalived服务器的一个标识，是发邮件时显示在邮件主题中的信息</span></div><div class="line">&#125;</div><div class="line"><span class="comment">#keepalived的VRRPD配置，是所有keepalived配置的核心</span></div><div class="line"><span class="comment">#VRRP实例配置</span></div><div class="line">vrrp_instance VIP_142 <span class="comment">#是VRRP实例开始的标识，后跟VRRP实例名称</span></div><div class="line">&#123;</div><div class="line">state MASTER <span class="comment">#keepalived的角色，MASTER主，BACKUP备</span></div><div class="line">interface eth0 <span class="comment">#用于指定HA监测网络的接口</span></div><div class="line">virtual_router_id 142 <span class="comment">#虚拟路由标识，这个标识是一个数字，同一个VRRP实例使用唯一的一个标识，即在同一个vrrp_instance下，MASTER和BACKUP必须是一致的！</span></div><div class="line">priority 100 <span class="comment">#权重优先级</span></div><div class="line">advert_int 2 <span class="comment">#用于设定master和backup主机之间同步检查的时间间隔，单位是秒</span></div><div class="line">garp_master_delay 10 <span class="comment">#用于切换到master状态后延时进行Gratuitous arp请求的时间</span></div><div class="line">smtp_alert <span class="comment">#表示是否开启邮件通知(用全局区域的邮件设置来发通知)</span></div><div class="line">authentication <span class="comment">#主备之间进行通信的验证类型和密码：验证类型主要有PASS和AH两种，一个在vrrp_instance下，MASTER和backup必须使用相同的密码才可以通信</span></div><div class="line">&#123;</div><div class="line">auth_type PASS</div><div class="line">auth_pass 123456</div><div class="line">&#125;</div><div class="line"><span class="comment">#virtual_ipaddress用于设置虚拟ip地址，可以设置多个vip，每行一个，</span></div><div class="line">virtual_ipaddress</div><div class="line">&#123;</div><div class="line">10.95.0.200/24</div><div class="line">&#125;</div><div class="line">track_interface <span class="comment">#用于设置一些额外的网络监控接口，其中任何一个网络接口出现故障，keepalived都会进去fault状态！</span></div><div class="line">&#123;</div><div class="line">eth0</div><div class="line">&#125;</div><div class="line">nopreempt <span class="comment">#设置不抢占功能，只能在backup上使用，知道机器有故障了才切换，</span></div><div class="line">preemtp_delay 300 <span class="comment">#用于设置抢占的延时时间，（例：开启启动没必要抢占）</span></div><div class="line">&#125;</div><div class="line"><span class="comment">#以下是lvs的主要主要配置信息，主要实现lvs的ip包转发功能！</span></div><div class="line">virtual_server 10.95.0.200 80 <span class="comment">#虚拟ip和端口</span></div><div class="line">&#123;</div><div class="line">delay_loop 6 <span class="comment">#设置健康检查的时间间隔</span></div><div class="line">lb_algo wrr <span class="comment">#设置负载调度算法</span></div><div class="line">lb_kind DR <span class="comment">#设置lvs的模式</span></div><div class="line">persistence_timeout 60 <span class="comment">#会话保持时间，单位秒</span></div><div class="line">protocol TCP <span class="comment">#ip包转发协议，有TCP和UDP两种</span></div><div class="line">real_server 10.95.0.143 80 <span class="comment">#real server 的ip</span></div><div class="line">&#123;</div><div class="line">weight 3 <span class="comment">#权重</span></div><div class="line">TCP_CHECK <span class="comment">#健康检查</span></div><div class="line">&#123;</div><div class="line">connect_timeout 10 <span class="comment">#表示无响应超时时间</span></div><div class="line">nb_get_retry 3 <span class="comment">#表示重连次数</span></div><div class="line">delay_before_retry 3 <span class="comment">#表示重试间隔</span></div><div class="line">connect_port 80 <span class="comment">#表示端口</span></div><div class="line">&#125;</div><div class="line">&#125;</div><div class="line">real_server 10.95.0.144 80</div><div class="line">&#123;</div><div class="line">weight 3</div><div class="line">TCP_CHECK</div><div class="line">&#123;</div><div class="line">connect_timeout 10</div><div class="line">nb_get_retry 3</div><div class="line">delay_before_retry 3</div><div class="line">connect_port 80</div><div class="line">&#125;</div><div class="line">&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;健康监测应许多种检查方式，常见的有，HTTP_GET，SSL_GET，TCP_CHECK，SMTP_CHECK，MISC_CHECK.</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">TCP_CHECK &#123;</div><div class="line">conetct_port 80</div><div class="line">connect_timeout 3</div><div class="line">nb_get_retry 3</div><div class="line">delay_before_retry 3</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;connect_port:健康检查的端口，如果不指定，默认是real_server指定的端口</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;connect_timeout:表示无响应超时时间，单位是秒，这里是3s</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;nb_get_retry：表示重试次数，这里是3ci</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;delay_before_retry：表示重试间隔，</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">HTTP_GET |SSL_GET</div><div class="line">&#123;</div><div class="line">url&#123;</div><div class="line">path /index.html <span class="comment">#指定url信息</span></div><div class="line">digest e6owjfdsjfalsjdfsalkf30wfdsfjwqe</div><div class="line"><span class="comment">#ssl检查后的摘要信息，这些摘要信息可以通过genhash命令工具获取，</span></div><div class="line"><span class="comment">#例：genhash -s 192.168.12.80 -p 80 -u /index.html</span></div><div class="line">status_code 200</div><div class="line">&#125;</div><div class="line">connect_port 80</div><div class="line">bindto 192.168.31.128 <span class="comment">#表示通过此地址来对发送请求对服务器进行健康检查</span></div><div class="line">nb_get_retry 3</div><div class="line">delay_before_retry 2</div><div class="line"></div><div class="line">&#125;</div></pre></td></tr></table></figure>
	

	
		<span class="different-posts"><a href="/2017/10/12/LVS/7. keepalived 和 lvs 的结合配置文件详解/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
